﻿using System;
using Castle.DynamicProxy;
using RrslLite.Net.Messages;
using RrslLite.Net.Proxies;

namespace RrslLite.Net.Aop
{
    public class InterceptorSync<TTarget> : InterceptorBase<TTarget>
            where TTarget : class
    {
        private readonly string _endpointConfigurationName;

        public InterceptorSync(string endpointConfigurationName)
            : base(new Message(Guid.NewGuid()))
        {
            _endpointConfigurationName = endpointConfigurationName;
        }

        protected override void ProcessMessage(Message message, IInvocation invocation)
        {
            var proxy = new MessageProcessorProxy(_endpointConfigurationName);

            invocation.ReturnValue = proxy.InvokeOperations(new[] { message })[0].Result;
        }
    }
}
